/*
 * IDesktopProtocol.java
 *
 * Created on July 30, 2007, 11:00 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package org.mobileandembedded.idesktop;

import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

/**
 *
 * @author helio.rodrigues
 */
public class IDesktopProtocol {

    public static final String AUTHENTICATION_MESSAGE = "000";
    public static final String AUTHENTICATION_SUCCESSFUL_MESSAGE = "001";
    public static final String AUTHENTICATION_FAILED_MESSAGE = "002";
    public static final String REQUEST_SCREENSHOT_MESSAGE = "003";
    public static final String DEVICE_CONFIGURATION_MESSAGE = "004";
    public static final String SCREENSHOT_MESSAGE = "005";
    public static final String MOUSE_LEFT_CLICK_MESSAGE = "006";
    public static final String MOUSE_RIGHT_CLICK_MESSAGE = "007";
    public static final String MOUSE_LEFT_PRESS_MESSAGE = "008";
    public static final String MOUSE_RIGHT_PRESS_MESSAGE = "009";
    public static final String MOUSE_LEFT_RELEASE_MESSAGE = "010";
    public static final String MOUSE_RIGHT_RELEASE_MESSAGE = "011";
    public static final String MOUSE_MOVE_MESSAGE = "013";
    public static final String KEYBOARD_INPUT_MESSAGE = "012";

    /**
     * Creates a new instance of IDesktopProtocol
     */
    public IDesktopProtocol() {

    }

    /**
     *
     * @param message
     * @return
     */
    public static byte[] getParameters(byte[] message) {
        byte[] params = new byte[message.length - 3];

        for (int i = 3; i < message.length; i++) {
            params[i - 3] = message[i];
        }

        return params;
    }

    /**
     *
     * @param message
     * @return
     */
    public static String getOperation(byte[] message) {
        String operation = "";

        for (int i = 0; i < 3; i++) {
            operation += ((char) message[i]) + "";
        }

        return operation;
    }

    /**
     *
     * @param inputStream
     * @throws java.io.IOException
     * @return
     */
    public static byte[] readMessage(InputStream inputStream) throws IOException {
        /* reads the message size */
        byte[] size = new byte[6];

        for (int i = 0; i < size.length; i++) {
            size[i] = (byte) inputStream.read();
        }

        /* reads the message */
        byte[] message = new byte[Integer.parseInt(new String(size))];

        for (int i = 0; i < message.length; i++) {
            message[i] = (byte) inputStream.read();
        }

        return message;
    }

    /**
     *
     * @param bytes
     * @return
     */
    public static Vector parseParameters(byte[] bytes) {
        String params = new String(bytes);
        Vector vector = new Vector();

        while (params.indexOf("&") >= 0) {
            vector.addElement(params.substring(0, params.indexOf("&")));
            params = params.substring(params.indexOf("&") + 1);
        }

        vector.addElement(params);
        return vector;
    }

    /**
     *
     * @param operation
     * @param params
     * @return
     */
    public static byte[] createMessage(String operation, Vector params) {
        String string = "";

        for (int i = 0; i < params.size(); i++) {
            string += params.elementAt(i) + "&";
        }

        string = string.substring(0, string.length() - 1);
        return createMessage(operation, string);
    }

    /**
     *
     * @param operation
     * @param params
     * @return
     */
    public static byte[] createMessage(String operation, String params) {
        return createMessage(operation, params.getBytes());
    }

    /**
     *
     * @param operation
     * @param params
     * @return
     */
    public static byte[] createMessage(String operation, byte[] params) {
        /* creates the message */
        String size = (operation.getBytes().length + params.length) + "";
        String zeros = "";

        for (int i = 0; i < 6 - size.length(); i++) {
            zeros += "0";
        }

        size = zeros + size;

        /* converts everything to bytes */
        byte[] message = new byte[size.getBytes().length + operation.getBytes().length + params.length];
        int b = 0;

        for (int i = 0; i < size.getBytes().length; i++) {
            message[b] = size.getBytes()[i];
            b++;
        }

        for (int i = 0; i < operation.getBytes().length; i++) {
            message[b] = operation.getBytes()[i];
            b++;
        }

        for (int i = 0; i < params.length; i++) {
            message[b] = params[i];
            b++;
        }

        return message;
    }
}
